package com.anhtuan.reminder.services;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

public class NotifyClient {
	private NotifyService boundService;
	private Context context;
	private boolean isBound;
	
	public NotifyClient(Context context) {
		this.context = context;
	}
	
	public void doBindService() {
		context.bindService(new Intent(context, NotifyService.class), connection, Context.BIND_AUTO_CREATE);
		isBound = true;
	}
	
	public void doUnbindService() {
		if (isBound) {
			context.unbindService(connection);
			isBound = false;
		}
	}
	
	private ServiceConnection connection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			boundService = null;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			boundService = ((NotifyService.ServiceBinder) service).getService();			
		}
	};
}
